<?php

/**
 * @Author: Ron Chen
 * @Date:   2016-12-05 21:29:35
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-02-08 21:04:28
 */

namespace common\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\web\UploadedFile;
use yii\helpers\Url;

class BaseModels extends ActiveRecord
{
    const GOODS_DATA            = "产品信息";
    const GOODS_PACK            = "保障套餐";

    const KEY_KGB               = "kgb";

    public static $key = [
        self::KEY_KGB           => "kgb",
    ];

    const MODEL_IS_VALID        = 1;
    const MODEL_NOT_VALID       = 0;

    public static $valid_list = [
        self::MODEL_IS_VALID    => "有效",
        self::MODEL_NOT_VALID   => "无效",
    ];

    const STATUS_NOT            = -1;
    const STATUS_YES            = 0;

    public static $status = [
        self::STATUS_NOT        => "删除",
        self::STATUS_YES        => "正常",
    ];

	/**
     * 默认更新时间
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value'=> time(),
            ],
        ];
    }

    /**
     * 获取静态资源地址
     */
    public function getStaticUrl($filename){
        $dir = sprintf('@web/%s',$filename);
        return Url::to($dir, true);
    }

    /**
     * Base64 上传文件操作
     */
    public function uploadImage($filename='',$name='image'){
        $image = UploadedFile::getInstance($this, $name);
        
        if (empty($image)) {
            return false;
        }
        $this->mkFiledir($filename);
        $image_content = "data:{$image->type};base64," . chunk_split(base64_encode(file_get_contents($image->tempName)));
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $image_content, $result)){
            $file_name = sprintf(".%s.%s",$filename,$result[2]);
            if (file_put_contents($file_name, base64_decode(str_replace($result[1], '', $image_content)))){
                return $image;
            }
        }

        return false;
    }

    /**
     * 文件流处理文件上传
     */
    public function moveImage($filename,$name='image'){
        $image = UploadedFile::getInstance($this, $name);
        if (empty($image)) {
            return false;
        }

        // $this->mkFiledir($filename);

        $file_name = sprintf(".%s.%s",$filename,$image->getExtension());
        if (!file_exists($file_name)) {
            move_uploaded_file($image,$file_name);
            return $image;
        }

        return false;
    }

    /**
     * 格式化时间
     * @params created_at 时间戳
     * @params type       显示格式
     */
    public function formatTime($created_at,$type='s'){
        $show_time = "-";
        if ($created_at) {
            switch ($type) {
                case 's':
                    $show_time = date("Y-m-d H:i:s",$created_at);
                    break;
                case 'd':
                    $show_time = date("Y-m-d",$created_at);
                    break;
                default:
                    $show_time = date("Y-m-d H:i:s",$created_at);
                    break;
            }
        }
        return $show_time;
    }

    /**
     * 根据路径文件名获取路径
     * 没有起效
     */
    private function mkFiledir($filename){
        $dir_file = dirname($filename);
        if (!file_exists($dir_file)) {
            mkdir($dir_file,'0777',true);
        }
    }

}